package ch.be.coop.unterhaltLaden.presentation.framesPanels;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.be.coop.unterhaltLaden.busines.CoopStore;
import ch.be.coop.unterhaltLaden.busines.Engine;

public class SerchPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Engine engine;
	private JLabel space, serchtitle;
	private JTextField serchfield;
	private Serchresult serchresult;
	private OrderPanel orderPanel;

	public SerchPanel(Engine engine) {
		
		this.setPreferredSize(new Dimension(250, 450));
		this.engine = engine;
		this.serchtitle = new JLabel("Suchen");
		this.space = new JLabel();
		this.serchfield = new JTextField();
		this.serchresult = new Serchresult();

		this.serchfield.addKeyListener(new SerchFieldListener(serchresult));

		this.orderPanel = new OrderPanel();

		this.setLayout(new BorderLayout());

		this.add(orderPanel, BorderLayout.NORTH);
		this.add(serchresult, BorderLayout.WEST);

	}

	private class OrderPanel extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public OrderPanel() {

			this.setLayout(new GridLayout(3, 1));

			add(space);
			add(serchtitle);
			add(serchfield);
		}
	}

	private class Serchresult extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private JList jList;
		private DefaultListModel defaultListModel;

		public Serchresult() {

			jList = new JList();

			defaultListModel = new DefaultListModel();

			jList.setModel(defaultListModel);

			MouseListListener listListener = new MouseListListener(jList);
			
			jList.addMouseListener(listListener);

			add(jList);

		}

		public JList getjList() {
			return jList;
		}

		public DefaultListModel getDefaultListModel() {
			return defaultListModel;
		}

	}

	private class MouseListListener implements MouseListener {

		private JList jList;

		public MouseListListener(JList jList) {

			this.jList = jList;
		}

		@Override
		public void mouseClicked(MouseEvent arg0) {
			

			new InfoMask(engine, (Integer.parseInt(getInfostring(jList.getSelectedValue()).getInternNumber()))-1);
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
		}

		private CoopStore getInfostring(Object selectedo) {
			System.out.println(selectedo);

			String selected = ""+selectedo;
			
			for (CoopStore cs : engine.getCoopList()) {
				if (selected.equals(cs.getName())) {
					return cs;
				}
			}
			return engine.getCoopList().get(12);
		}

	}

	private class SerchFieldListener implements KeyListener {

		private Serchresult serchresult;

		public SerchFieldListener(Serchresult serchresult) {
			this.serchresult = serchresult;
		}

		@Override
		public void keyPressed(KeyEvent arg0) {

		}

		private ArrayList<CoopStore> checkSerchvalue(String serchvalue) {

			ArrayList<CoopStore> temp = new ArrayList<CoopStore>();

			for (CoopStore store : engine.getCoopList()) {
				if (serchvaluepart(serchvalue.toLowerCase(), store.getName().toLowerCase())) {
					temp.add(store);
				}

			}
			return temp;

		}

		private boolean serchvaluepart(String serchvaluePart, String storevalue) {

			int leng = serchvaluePart.length();

			if (storevalue.length() >= serchvaluePart.length()) {
				
				if (storevalue.substring(0, leng).equals(serchvaluePart)) {
					return true;
				}

			}

			return false;
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			
			this.serchresult.getDefaultListModel().removeAllElements();
			
			ArrayList<CoopStore> arrayList = new  ArrayList<CoopStore>();

			arrayList = checkSerchvalue(serchfield.getText().toString());

			for (CoopStore coopStore : arrayList) {
				
				this.serchresult.getDefaultListModel().addElement(coopStore);
			}
		}

		@Override
		public void keyTyped(KeyEvent arg0) {

		}

	}

}
